package com.example.sixteen.jt.lib;

import java.util.HashMap;
import java.util.Map;

public class Reader {

    public static final int SUCCESS = 0;

    static {
        System.loadLibrary("jt");
    }

    private long handle;

    private String ip;

    private Map<Integer, Integer> mapAntennaBox = new HashMap<>();

    public void setMapAntennaBox(Map<Integer, Integer> mapAntennaBox) {
        this.mapAntennaBox.clear();
        for (Integer key : mapAntennaBox.keySet()) {
            this.mapAntennaBox.put(key, mapAntennaBox.get(key));
        }
    }

    protected void onAlive() {
        if (listener != null) {
            listener.onAlive(ip);
        }
    }

    protected void onInventory(String epc, int ant, int rssi, int high) {
        if (listener != null) {
            if (this.mapAntennaBox.containsKey(Integer.valueOf(ant))) {
                int box = this.mapAntennaBox.get(Integer.valueOf(ant));
                listener.onInventory(ip, epc, ant, box, rssi, high == 1);
            }
        }
    }

    protected void onStop() {
        if (listener != null) {
            listener.onStop(ip);
        }
    }

    private OnReaderListener listener;

    public void setOnReaderListener(OnReaderListener listener) {
        this.listener = listener;
    }

    public interface OnReaderListener {
        /**
         * 心跳
         */
        void onAlive(String ip);

        /**
         * 盘点结果
         */
        void onInventory(String ip, String epc, int ant, int box, int rssi, boolean high);

        /**
         * 盘点结束
         */
        void onStop(String ip);
    }

    public native int connect(String ip, int port);

    public native int disconnect();

    public native int inventory(int times);

    public native int light(byte[] epc, int len);

    public native int setAnt(JTAnt[] ants);

    public native int getAnt(JTAnt[] ants);

    /**
     * 同时点亮多个发光标签
     * @param marks
     * @param times
     * @return
     */
    public native int startLightMulti(SelMark[] marks, int times);

    /**
     * 停止点亮多个发光标签
     * @return
     */
    public native int stopLightMulti();

    public native int setFreq(JTFreq freq);

    public native int getFreq(JTFreq freq);
}
